package com.gitee.feizns.explore.data_structure.tree.bs.mid;

import java.util.Stack;

/**
 * @author feizns
 * @since 2020/3/9
 */
public class BSTIterator {

    Stack<TreeNode> stack = new Stack<>();

    public BSTIterator(TreeNode root) {
        stack.push(root);
        while ( root.left != null ) {
            stack.push(root.left);
            root = root.left;
        }
    }

    public int next() {
        TreeNode ret = stack.pop();
        if ( ret.right != null ) {
            TreeNode tmp = ret.right;
            while ( tmp != null ) {
                stack.push(tmp);
                tmp = tmp.left;
            }
        }
        return ret.val;
    }

    public boolean hasNext() {
        return !stack.isEmpty();
    }

}
